package com.sduept.nwld.dataserver.manager.message.interfaces.imp;

import com.sduept.message.entity.AlarmMessage;
import com.sduept.message.entity.AlarmMessageWrapper;
import com.sduept.message.interfaces.MessageSendInterface;
import com.sduept.message.manager.MessageReceiveConfigManager;
import com.sduept.message.observer.MessageSendObserver;
import com.sduept.nwld.dataserver.exceptions.MessageException;
import com.sduept.nwld.dataserver.manager.message.EspaceMessagePushManager;
import com.sduept.permission.entity.PersonnelEntityBean;
import com.sduept.permission.manager.PersonnelManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.*;

/**
 * @author Author
 * @version V1.0
 * @Date 2018年8月23日 下午2:55:36
 */

@Service
public class EspaceMessageObserver implements MessageSendInterface {

	private static final String MESSAGE_RECEIVER_MESSAGE = "message";

	@Autowired
	private MessageSendObserver messageSendObserver;
	@Autowired
	private MessageReceiveConfigManager messageReceiveConfigManager;
	@Autowired
	private PersonnelManager personnelManager;
	@Autowired
	private EspaceMessagePushManager espaceMessagePushManager;

	@PostConstruct
	public void init() {
		register();//实现类监听注册
	}

	@Override
	public void alarmMessageSend(AlarmMessageWrapper alarmMessageWrapper) {
		AlarmMessage alarmMessage = alarmMessageWrapper.getAlarmMessage();//告警消息
		Set<String> givenCodes = new HashSet<String>();//指定人员
		givenCodes.addAll(alarmMessageWrapper.getCodes());
		Set<String> codeSet = new HashSet<String>();//code集合
		//无论是否指定人员，均读取个人消息配置
		List<String> codeConfig = messageReceiveConfigManager.listConfigureIds(alarmMessage.getMsgType(), MESSAGE_RECEIVER_MESSAGE);
		if (null != codeConfig && codeConfig.size() >= 0) {
			codeSet.addAll(codeConfig);
		}
		if(null != givenCodes && givenCodes.size() > 0) {//指定人员
			if(alarmMessageWrapper.isForce()){//强制发送
				codeSet.addAll(givenCodes);
			}else{//不强制发送，查取有此接收方式配置的人员，把givenCodes中没配置此消息的人员去除
				Iterator<String> it = givenCodes.iterator();
				while(it.hasNext()){
					String code = it.next();
					if(!codeConfig.contains(code)){//有权限接收此消息的人员列表中无给定人呿
						it.remove();
					}
				}
				codeSet.addAll(givenCodes);
			}
		}
		String[] array = codeSet.toArray(new String[codeSet.size()]);
		Map<String, String> personMap = findPhonesByCodes(array);//<手机号，用户名>
		if(personMap.size() == 0) {
			return;
		}
		try {
			espaceMessagePushManager.messagePush(personMap, alarmMessage.getContent());
		} catch (MessageException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据codes查询人员的手机号、用户名
	 * @param codesArr codes数组
	 * @return 人员手机号
	 */
	private Map<String, String> findPhonesByCodes(String[] codesArr) {
		Map<String, String> personMap = new HashMap<String, String>();// <手机号，用户名>
		List<PersonnelEntityBean> personList = personnelManager.findPersonnelByCodes(codesArr);//根据codes查询人员信息
		for (PersonnelEntityBean person : personList) {
			if (StringUtils.isEmpty(person.getTelephone()) || StringUtils.isEmpty(person.getName())) {
				continue;
			}
			personMap.put(person.getTelephone(), person.getName());
		}
		return personMap;
	}

	@Override
	public void register() {
		messageSendObserver.register(this);
	}

}
